package cn.lf.android.lawfirm.home.controller;

import android.content.Context;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.home.modle.UserInfo;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.login.UserAccount;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;


public class QuitController extends BaseController {
    QuitCallback mQuitCallback;
    private Context context;

    public QuitController(QuitCallback callback, Context context) {
        this.mQuitCallback = callback;
        this.context = context;
    }

    public void onQuit() {
        showProgressDia(context);
        SPUtil spUtil = new SPUtil(context);
        UserAccount userAccount = spUtil.getUserAccount();
        final String u = userAccount.getUserName();
        Map<String, String> params = new HashMap<String, String>();
        params.put("Authorization", spUtil.getAccessToken());
        new MyOkHttpClient().doPost(AppConfig.Quit, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("=退出66==" + response);
                        JSONObject myJsonObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                UserAccount userAccount = new UserAccount();
                                UserInfo userInfo = new UserInfo();
                                SPUtil spUtil = new SPUtil(context);
                                spUtil.setAccountInfo(userAccount);
                                spUtil.setAccessToken(null);
                                spUtil.setUserInfo(userInfo);
                                mQuitCallback.onQuitSuccessed(u);
                            } else {
                                mQuitCallback
                                        .onQuitFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        DebugLog.e("=退出失败66==" + errorMsg);
                        mQuitCallback.onQuitFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }

    public interface QuitCallback {
        public void onQuitSuccessed(String str);

        public void onQuitFailed();
    }
}
